package com.nuanshui.framework.utils;

import com.alibaba.fastjson.JSON;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

public class ObjectUtil {

    public static Map<String, Object> createParamsByJson(Object object) {
        String jsonString = JSON.toJSONString(object);
        return JSON.parseObject(jsonString);
    }

    private static String javaType = "java";

    public static Map<String, Object> createParams(Object object) throws Exception {
        Map<String, Object> params = new HashMap<String, Object>();

        setIntoParams(params, object, null);
        return params;
    }

    private static void setIntoParams(Map<String, Object> params, Object object, String fatherName) throws IllegalAccessException,
            Exception {
        Field[] fields = object.getClass().getDeclaredFields();
        for (Field file : fields) {
            boolean accessFlag = file.isAccessible();
            file.setAccessible(true);
            String name = file.getName();
            Object value = file.get(object);

            if (file.getType().getName().equals("java.lang.Class")) {
                break;
            } else if (file.getType().getName().contains(javaType)) {
                if (fatherName != null && !fatherName.equals(" ")) {
                    name = fatherName + "." + name;
                }

                if (value != null) {
                    params.put(name, value + "");
                }


            } else {
//                if(value != null){
//                    setIntoParams(params,file.get(object), name);
//                }

            }
            file.setAccessible(accessFlag);
        }
    }
}
